package com.aistar.controller;

import com.aistar.pojo.*;
import com.aistar.service.*;
import com.aistar.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/dynamic")
public class DynamicPraiseAndCollectController {
    private final CustomerService customerService;
    private final DynamicPraiseService dynamicPraiseService;
    private final DynamicCollectService dynamicCollectService;
    private final DynamicService dynamicService;
    private final MessageService messageService;

    @Autowired
    public DynamicPraiseAndCollectController(CustomerService customerService,
                                             DynamicPraiseService dynamicPraiseService,
                                             DynamicCollectService dynamicCollectService,
                                             DynamicService dynamicService,
                                             MessageService messageService) {
        this.customerService = customerService;
        this.dynamicPraiseService = dynamicPraiseService;
        this.dynamicCollectService = dynamicCollectService;
        this.dynamicService = dynamicService;
        this.messageService = messageService;
    }

    @RequestMapping("/like/{customerId}/{dynamicId}")
    @ResponseBody
    public ServerResponse like(@PathVariable("customerId") Integer customerId, @PathVariable("dynamicId") Integer dynamicId) {
        String field = customerId + RedisMessage.SEPARATOR + dynamicId;
        String value = (String) RedisUtil.hget(RedisMessage.DYNAMIC_PRAISE_KEY, field);
        int valueType;
        if (value == null) {
            ServerResponse serverResponse = dynamicPraiseService.getByCidAndPid(customerId, dynamicId);
            if (MessageUtil.STATUS_SUCCESS.equals(serverResponse.getStatus()))
                valueType = ((DynamicPraise) serverResponse.getData()).getStatus() == 0 ? 1 : 0;
            else {
                valueType = 1;
                Customer customer = (Customer) customerService.getById(customerId).getData();
                Dynamic dynamic = (Dynamic) dynamicService.getById(dynamicId).getData();
                Message message = new Message();
                message.setCustomerId(dynamic.getCustomerId());
                message.setMessageContent(customer.getCustomerName() + "赞了你的动态");
                message.setMessageType(MessageUtil.PUSH_USER);
                messageService.add(message);
            }
        } else
            valueType = value.split(RedisMessage.SEPARATOR)[0].equals("0") ? 1 : 0;
        value = valueType + RedisMessage.SEPARATOR + new Date().getTime();
        if (RedisUtil.hset(RedisMessage.DYNAMIC_PRAISE_KEY, field, value))
            return ServerResponse.modifySuccess(valueType == 1 ? 1 : -1);
        return ServerResponse.modifyFailed();
    }

    @RequestMapping("/collect/{customerId}/{dynamicId}")
    @ResponseBody
    public ServerResponse collect(@PathVariable("customerId") Integer customerId, @PathVariable("dynamicId") Integer dynamicId) {
        String field = customerId + RedisMessage.SEPARATOR + dynamicId;
        String value = (String) RedisUtil.hget(RedisMessage.DYNAMIC_COLLECT_KEY, field);
        int valueType;
        if (value == null) {
            ServerResponse serverResponse = dynamicCollectService.getByCidAndPid(customerId, dynamicId);
            if (MessageUtil.STATUS_SUCCESS.equals(serverResponse.getStatus()))
                valueType = ((DynamicCollect) serverResponse.getData()).getStatus() == 0 ? 1 : 0;
            else {
                valueType = 1;
                Customer customer = (Customer) customerService.getById(customerId).getData();
                Dynamic dynamic = (Dynamic) dynamicService.getById(dynamicId).getData();
                Message message = new Message();
                message.setCustomerId(dynamic.getCustomerId());
                message.setMessageContent(customer.getCustomerName() + "收藏了你的动态");
                message.setMessageType(MessageUtil.PUSH_USER);
                messageService.add(message);
            }
        } else
            valueType = value.split(RedisMessage.SEPARATOR)[0].equals("0") ? 1 : 0;
        value = valueType + RedisMessage.SEPARATOR + new Date().getTime();
        if (RedisUtil.hset(RedisMessage.DYNAMIC_COLLECT_KEY, field, value))
            return ServerResponse.modifySuccess(valueType == 1 ? 1 : -1);
        return ServerResponse.modifyFailed();
    }

}
